﻿using System.Windows.Forms;
using System.IO;
using System.Data;




namespace AvvaMobileNHibernateHelperKit
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// 
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
            this.Text = this.Text.Replace("{ProjectName}", ProjectSettings.ProjectName);
        }

        #region Private Fields
        
        DataTable dtErrors = new DataTable();

        #endregion

        #region Public Properties

        /// <summary>
        /// 
        /// </summary>
        public string Status
        {
            get{ return this.statusBar.Text; }
            set{ this.statusBar.Text = value; }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Opens selected project.
        /// </summary>
        /// <param name="projectSolutionFile"></param>
        public void OpenProject(string projectSolutionFile)
        {
            Project.LoadProject(projectSolutionFile);
            AvvaMobileNHibernateHelperKit.Controls.Designer designerPage = new AvvaMobileNHibernateHelperKit.Controls.Designer();
            designerPage.Dock = DockStyle.Fill;
            this.pnlContent.Controls.Clear();
            this.pnlContent.Controls.Add(designerPage);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="errorType"></param>
        /// <param name="Description"></param>
        public void ErrorOccurred(ErrorTypes errorType, string Description, bool clearOldErrors)
        {            
            const int ErrorsListColumnsCount = 3;
            if (dtErrors.Columns.Count != ErrorsListColumnsCount)
            {
                dtErrors.Columns.Clear();
                dtErrors.Columns.Add(new DataColumn("ErrorType"));
                dtErrors.Columns.Add(new DataColumn("Description"));
                dtErrors.Columns.Add(new DataColumn("ProjectName"));
            }

            if(clearOldErrors)
            {
                this.dtErrors.Rows.Clear();
            }
            
            DataRow dr = dtErrors.NewRow();
            dr["ErrorType"] = errorType;
            dr["Description"] = Description;
            dr["ProjectName"] = errorType;
            dtErrors.Rows.Add(dr);
        }

        /// <summary>
        /// 
        /// </summary>
        public void BindErrors()
        {
            errorListWindow1.ErrorList = dtErrors;
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, System.EventArgs e)
        {
            ApplicationSettings.ApplicationStartupPath = Application.StartupPath;

            CreateSystemFolders();

            CheckSystemFiles();

            ShowStartPage();
            //ShowDesignerPage();

            ddlRunMode.SelectedIndex = 0;

            //ErrorOccurred(ErrorTypes.Error, "ErrorDeneme", false);
            //ErrorOccurred(ErrorTypes.Warning, "WarningDeneme", false);
            //ErrorOccurred(ErrorTypes.Message, "MessageDeneme", false);
            //BindErrors();
            
        }       

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imgOutPutButton_MouseEnter(object sender, System.EventArgs e)
        {
            this.outPutWindow1.ShowOutput(true);
            this.outPutWindow1.BringToFront();
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuExit_Click(object sender, System.EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imgErrorList_MouseEnter(object sender, System.EventArgs e)
        {
            this.errorListWindow1.ShowOutput();
            this.errorListWindow1.BringToFront();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imgToolBox_MouseEnter(object sender, System.EventArgs e)
        {
            this.toolBoxWindow1.ShowOutput();
            this.toolBoxWindow1.BringToFront();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imgProperties_MouseEnter(object sender, System.EventArgs e)
        {
            this.propertiesWindow1.ShowOutput();
            this.propertiesWindow1.BringToFront();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Resize(object sender, System.EventArgs e)
        {
            propertiesWindow1.ParentContainerWidth = this.pnlMain.Width;
        }
       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuAbout_Click(object sender, System.EventArgs e)
        {
            new AboutBox().ShowDialog();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Check and creates system folders.
        /// </summary>
        private void CreateSystemFolders()
        {
            //Check and creates system settings folder.
            if(!Directory.Exists(ApplicationSettings.ApplicationStartupPath + @"\Settings\"))
            {
                Directory.CreateDirectory(ApplicationSettings.ApplicationStartupPath + @"\Settings\");
            }
        }

        /// <summary>
        /// Check and creates system and settings files.
        /// </summary>
        private void CheckSystemFiles()
        {
            if(!File.Exists(ApplicationSettings.ApplicationStartupPath + @"\Settings\" + SystemFileNames.RECENT_PROJECTS))
            {
                DataTable dt = new DataTable("RecentProjects");
                dt.Columns.Add(new DataColumn("ProjectID"));
                dt.Columns.Add(new DataColumn("ProjectName"));
                dt.Columns.Add(new DataColumn("ProjectTypeID"));
                dt.Columns.Add(new DataColumn("ProjectSolutionFile"));
                DataRow dr = dt.NewRow();
                dr["ProjectID"] = "1";
                dr["ProjectName"] = "Test Project Name";
                dr["ProjectTypeID"] = "1";
                dr["ProjectSolutionFile"] = @"c:\Test\testSolution.sln";
                dt.Rows.Add(dr);
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                ds.WriteXml(ApplicationSettings.ApplicationStartupPath + @"\Settings\" + SystemFileNames.RECENT_PROJECTS);
            }

            if (!File.Exists(ApplicationSettings.ApplicationStartupPath + @"\Settings\" + SystemFileNames.CONNECTION_UDL))
            {
                File.Create(ApplicationSettings.ApplicationStartupPath + @"\Settings\" + SystemFileNames.CONNECTION_UDL);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuNewProject_Click(object sender, System.EventArgs e)
        {
            new CreateProjectDialog(this).ShowDialog();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuOpenProject_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Nh-Studio Solution File(*.slnx)|*.slnx";
            if(ofd.ShowDialog() == DialogResult.OK && ofd.FileName != null)
            {
                OpenProject(ofd.FileName);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void Save()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        private void SaveAll()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void newToolStripButton_Click(object sender, System.EventArgs e)
        {
            new CreateProjectDialog(this).ShowDialog();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openToolStripButton_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Nh-Studio Solution File(*.slnx)|*.slnx";
            if(ofd.ShowDialog() == DialogResult.OK && ofd.FileName != null)
            {
                OpenProject(ofd.FileName);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuToolBox_Click(object sender, System.EventArgs e)
        {
            this.toolBoxWindow1.ShowOutput();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuProperties_Click(object sender, System.EventArgs e)
        {
            this.propertiesWindow1.ShowOutput();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuErrorList_Click(object sender, System.EventArgs e)
        {
            this.errorListWindow1.ShowOutput();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuOutPut_Click(object sender, System.EventArgs e)
        {
            outPutWindow1.ShowOutput();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolBoxToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            this.toolBoxWindow1.ShowOutput();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void propertiesWindowToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            this.propertiesWindow1.ShowOutput();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void errorListToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
             this.errorListWindow1.ShowOutput();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void outputWindowToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
             this.outPutWindow1.ShowOutput();
        }

        /// <summary>
        /// 
        /// </summary>
        private void ShowStartPage()
        {
            //AvvaMobileNHibernateHelperKit.Controls.Designer startPage = new AvvaMobileNHibernateHelperKit.Controls.Designer();
            AvvaMobileNHibernateHelperKit.Controls.StartPage startPage = new AvvaMobileNHibernateHelperKit.Controls.StartPage();
            startPage.Dock = DockStyle.Fill;
            this.pnlMain.Controls["pnlContent"].Controls.Clear();
            this.pnlMain.Controls["pnlContent"].Controls.Add(startPage);
        }

        /// <summary>
        /// 
        /// </summary>
        private void ShowDesignerPage()
        {
            AvvaMobileNHibernateHelperKit.Controls.Designer designerPage = new AvvaMobileNHibernateHelperKit.Controls.Designer();
            //AvvaMobileNHibernateHelperKit.Controls.StartPage designerPage = new AvvaMobileNHibernateHelperKit.Controls.StartPage();
            designerPage.Dock = DockStyle.Fill;
            this.pnlMain.Controls["pnlContent"].Controls.Clear();
            this.pnlMain.Controls["pnlContent"].Controls.Add(designerPage);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void startPageToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            ShowStartPage();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuStartPage_Click(object sender, System.EventArgs e)
        {
            ShowStartPage();
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuOptions_Click(object sender, System.EventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ptionsToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            new Forms.ProjectProperties().ShowDialog();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, System.EventArgs e)
        {
            ShowDesignerPage();
        }


        #endregion





































    }
}